package com.car.service.impl.check;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.dao.check.checkDao;
import com.car.entity.Check;
import com.car.service.check.checkService;
import com.car.util.ResultMsg;
import com.car.util.SnowFlakeIdWorker;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class checkServiceImpl implements checkService {

    @Getter
    @Setter
    @Autowired
    checkDao checkDao;

    @Override
    public Page selectlistquery(Map<String, Object> map) {
        Integer pageNum = (Integer) map.get("pageNum");
        Integer pageSize = (Integer) map.get("pageSize");
        Page page = new Page((pageNum-1)*pageSize,pageSize);

        Integer pageIndex = (pageNum-1)*pageSize;
        map.put("pageIndex",pageIndex);
        Integer checkCount = checkDao.getChecksCountQuery(map);
        //设置起始位置
        List list = checkDao.selectListQuery(map);
        //先设置总条数
        page.setTotal(checkCount);
        //设置每页大小
        page.setSize(pageSize);
        //设置当前页
        page.setCurrent(pageNum);
        //设置内容
        page.setRecords(list);
        return page;

    }

    @Autowired
    SnowFlakeIdWorker snowFlakeIdWorker;
    @Override
    public ResultMsg addCheck(Check check) {
        long nextId =snowFlakeIdWorker.nextId();
        ResultMsg resultMsg = new ResultMsg();
        check.setCheckId("C"+nextId);
        int flag = checkDao.insert(check);
        if(flag>0){
            resultMsg.setMsg("添加成功,审核表id为："+check.getCheckId());
        }else {
            resultMsg.setMsg("添加失败");
        }
        resultMsg.setFlag(flag);
        return resultMsg;
    }

    @Override
    public ResultMsg delCheck(Check check) {
        ResultMsg resultMsg = new ResultMsg();
        int flag = checkDao.deleteById(check);
        if(flag>0){
            resultMsg.setMsg("删除成功");
        }else {
            resultMsg.setMsg("删除失败");
        }
        resultMsg.setFlag(flag);
        return resultMsg;
    }

    @Override
    public ResultMsg checkquery(Check check) {
        ResultMsg resultMsg = new ResultMsg();
        Check result = checkDao.selectById(check.getCheckId());
        if(result!=null){
            resultMsg.setMsg("查询成功");
            resultMsg.setFlag(1);
            resultMsg.setEntityObj(result);
        }else {
            resultMsg.setMsg("查询失败");
            resultMsg.setFlag(0);
        }
        return resultMsg;
    }

    @Override
    public ResultMsg updateCheck(Check check) {
        ResultMsg resultMsg = new ResultMsg();
        int  flag = checkDao.updateById(check);
        if(flag>0){
            resultMsg.setMsg("修改成功");
        }else {
            resultMsg.setMsg("修改失败");
        }
        resultMsg.setFlag(flag);
        return resultMsg;
    }

    @Override
    public Check selectCheckbyOrderId(String orderId) {
        LambdaQueryWrapper<Check> lw = new LambdaQueryWrapper<Check>();
        lw.eq(Check::getOrderID,orderId);
        return checkDao.selectOne(lw);
    }
}
